<?php

/**
 * @Author: Mr.Mao
 * @Date:   2021-05-25 16:25:07
 * @Last Modified by:   Mr.Mao
 * @Last Modified time: 2021-05-27 16:31:43
 */
namespace app\weixin\model;
use app\BaseModel;
use think\facade\Cache;
use think\Db;
//*------------------------------------------------------ */
//-- 视频号商品类目模型
/*------------------------------------------------------ */
class WeixinVideoGoodsModel extends BaseModel
{
	protected $table = 'weixin_video_goods';
	public  $pk = 'id';
	protected $mkey = 'weixin_video_goods_';
    public $goodsErrorCode = [1000001=>'未上传商品out_product_id',1000002=>'该商品正在审核中，请勿重复提交',1000003=>'该商品未经过审核，无可用的数据',1000004=>'新增sku需要调用商品接口，免审更新只支持已通过审核的SKU',1000005=>'该商品类目需要特殊资质，需先调用类目接口',1000006=>'该商品存在未授权的品牌信息，需先调用品牌接口',1000007=>'不正确的类目信息，类目id必须是三级类目',1000008=>'商品资质图片为必填字段，请补充',1000009=>'调用的类目id不存在',1000011=>'商品id不存在',1000012=>'商品id已存在，请调用更新接口',1000013=>'商品更新冲突',1000014=>'商品必填字段漏填',1000015=>'商品新增时sku库存不允许为0',1000016=>'商品不在审核中',1000017=>'推送审核失败，请调用商品更新接口完成推审'];
	/**
	 * [add 上传商品]
	 * @param array $param [商品信息]
	 */
	public function add($param=[])
	{
		$ACCESS_TOKEN = (new WeiXinMpModel)->getAccessToken();
        $url = "https://api.weixin.qq.com/shop/spu/add?access_token=$ACCESS_TOKEN";
        $result = $this->curlGoods($url,$param); 
        return $result;    
	}
	/**
	 * [getList 获取商品列表]
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	public function getList($param)
	{
		$ACCESS_TOKEN = (new WeiXinMpModel)->getAccessToken();
        $url = "https://api.weixin.qq.com/shop/spu/get_list?access_token=$ACCESS_TOKEN";
        $result = $this->curlGoods($url,$param); 
        return $result;
	}
	/**
	 * [updateGoods 更新商品]
	 * @param  string $param [更新参数]
	 * @return [type]        [description]
	 */
	public function updateGoods($param)
	{
		$ACCESS_TOKEN = (new WeiXinMpModel)->getAccessToken();
        $url = "https://api.weixin.qq.com/shop/spu/update?access_token=$ACCESS_TOKEN";
        $result = $this->curlGoods($url,$param); 
        return $result;
	}
    /**
     * [info 获取商品详情]
     * @param  [type] $param [请求参数]
     * @return [type]        [商品详情]
     */
    public function info($param)
    {
        $ACCESS_TOKEN = (new WeiXinMpModel)->getAccessToken();
        $url = "https://api.weixin.qq.com/shop/spu/get?access_token=$ACCESS_TOKEN";
        $result = $this->curlGoods($url,$param); 
        return $result;
    }
    /**
     * [delisting 下架商品]
     * @param  array $param [请求参数]
     */
    public function delisting($param)
    {
        $ACCESS_TOKEN = (new WeiXinMpModel)->getAccessToken();
        $url = "https://api.weixin.qq.com/shop/spu/delisting?access_token=$ACCESS_TOKEN";
        $result = $this->curlGoods($url,$param); 
        return $result;
    }
    /**
     * [delisting 上架商品]
     * @param  array $param [请求参数]
     */
    public function listing($param)
    {
        $ACCESS_TOKEN = (new WeiXinMpModel)->getAccessToken();
        $url = "https://api.weixin.qq.com/shop/spu/listing?access_token=$ACCESS_TOKEN";
        $result = $this->curlGoods($url,$param); 
        return $result;
    }
    /**
     * [delSpu 删除商品]
     * @param  array  $param [请求参数]
     */
    public function delSpu($param)
    {
        $ACCESS_TOKEN = (new WeiXinMpModel)->getAccessToken();
        $url = "https://api.weixin.qq.com/shop/spu/del?access_token=$ACCESS_TOKEN";
        $result = $this->curlGoods($url,$param); 
        return $result;
    }
    /**
     * [delAudit 撤回商品审核]
     * @param  array  $param [请求参数]
     */
    public function delAudit($param)
    {
        $ACCESS_TOKEN = (new WeiXinMpModel)->getAccessToken();
        $url = "https://api.weixin.qq.com/shop/spu/del_audit?access_token=$ACCESS_TOKEN";
        $result = $this->curlGoods($url,$param); 
        return $result;
    }

	/**
     * [curlGoods curl]
     * @param  string $url   [description]
     * @param  [type] $param [description]
     * @return [type]        [description]
     */
    public function curlGoods($url='',$param = array())
    {
    	$data = json_encode($param,JSON_UNESCAPED_UNICODE);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);
        if (curl_errno($ch)) {
            return curl_error($ch);
        }
        curl_close($ch);
        return json_decode($result,true);
    }
}